<?php

	class SiteHitsModel extends EbTableSQLModel
	{		
		private $listChartType;
		
		public function __construct()
		{
			parent::init("an_hits",'Analysis/SiteHitsVO',"hits_id");
			
			$this->listChartType = array('COUNT(*)','SUM(generated_time)/COUNT(*)','SUM(memory_used)/COUNT(*)');
		}
		
		public function getColumns()
		{
			return array(
				new EbTableColumn("hits_id","INT",true,0x01,false),
				new EbTableColumn("year","SMALLINT"),
				new EbTableColumn("month","SMALLINT"),
				new EbTableColumn("day","SMALLINT"),
				new EbTableColumn("hour","SMALLINT"),
				new EbTableColumn("timespan","INT"),
				new EbTableColumn("ro_id","INT"),
				new EbTableColumn("generated_time","SMALLINT"),
				new EbTableColumn("memory_used","SMALLINT"),
				new EbTableColumn("url","VARCHAR(128)")
			);
		}
		
		public function createData()
		{
			$model = EbModel::getModel('Analysis','SiteHits');
			
			EbApplication::$debug = false;
			
			for($i = 5; $i < 12; $i++)
			{
				$max = rand(10,100);
				
				for ($j = 0; $j < $max; $j++)
				{
					$d = rand(1,30);
					$h = rand(1,24);
					
					$y = Date('Y');
					$m = $i;
					$url = 'Jaja';
					$ref = 'Mon cul';
					$ro_id = rand(1,100);
					
					$model->insert((object)array('year' => $y,'month' => $m,'day' => $d,'hour' => $h,'url' => $url,'ro_id' => $ro_id));
					
					//echo "m=$m d=$d h=$h<br /><br />";
				}
			}
		}
		
		public function selectGroupByYear()
		{
			$sql = 'SELECT year,COUNT(*) AS value FROM an_hits GROUP BY year';
			
			return $this->execute($sql);
		}			
		
		public function selectGroupByMonth()
		{
			$sql = 'SELECT year,month,COUNT(*) AS value FROM an_hits GROUP BY year,month';
			
			return $this->execute($sql);
		}

		public function selectGroupByDay()
		{
			$sql = 'SELECT year,month,day,COUNT(*) AS value FROM an_hits GROUP BY year,month,day';
			
			return $this->execute($sql);
		}
		
		public function selectGroupByHour($data)
		{
			$data = (object) $data;
			
			$sql = 'SELECT year,month,day,hour AS time,'.$this->listChartType[$data->type].' AS value FROM an_hits GROUP BY year,month,day,hour';
			
			return $this->execute($sql);
		}		
	}